package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.UserLoginMsg;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Auth: 杨宇健
 * Time: 2023/1/10 15:20
 * Description: 订单查询
 **/

@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    /**
     * 获取当前用户订单
     * @param request
     * @param userId
     * @return
     */
    @Override
    public OrderListResponse orderList(OrderListRequest request, Long userId) {

        PageHelper.startPage(request.getPage(), request.getSize());
        List<Order> orders = orderMapper.selectAllOrderByUserId(userId);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);

        ArrayList<OrderDetailInfo> orderDetailInfos = new ArrayList<>();

        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);

            String orderId = order.getOrderId();

            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
//            Example example = new Example(OrderItem.class);
//            Example.Criteria criteria = example.createCriteria();
//             criteria.andEqualTo("order_id", orderId);
//            List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);

            orderDetailInfo.setOrderItemDto(orderItemDtos);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);

            orderDetailInfos.add(orderDetailInfo);
        }

        return new OrderListResponse(orderDetailInfos, orderPageInfo.getTotal());

    }

    // 查询订单详情
    @Override
    public OrderDetailResponse orderDetail(String orderId, UserLoginMsg user) {
        Order order = orderMapper.selectByPrimaryKey(orderId);



        if (order == null) {
            //订单不存在
            throw new BizException(OrderRetCode.INIT_ORDER_EXCEPTION.getCode(), OrderRetCode.INIT_ORDER_EXCEPTION.getMessage());
        }
        //TODO 杨宇健 判定订单token
        if (!order.getUserId().equals(user.getUid())) {
            throw new BizException(SysRetCodeConstants.GET_TOKEN_FAILED.getCode(), SysRetCodeConstants.GET_TOKEN_FAILED.getMessage());

        }

        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order);

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);

        if (orderItems == null || orderItems.isEmpty()) {
            //订单详情商品不存在
            throw new BizException(OrderRetCode.CALL_PRODUCT_DETAIL_FAIL.getCode(), OrderRetCode.CALL_PRODUCT_DETAIL_FAIL.getMessage());
        }

        List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);

        OrderDetailResponse orderDetailResponse = orderConverter.order2res(order, orderShipping);



        orderDetailResponse.setGoodsList(orderItemDtos);

        return orderDetailResponse;
    }

}
